<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台主页模块 - XzcBlogTemplate后台管理</title>
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="static/admin/css/common.css">
    <link rel="stylesheet" type="text/css" href="static/admin/css/blogadd.css">
</head>
<body>
<div class="layui-container main">
    <div class="layui-row layui-col-space20">
        <div class="layui-col-md12">
            <p>
		        	<span class="layui-breadcrumb" style="visibility: visible;">
			          	<a href="bloglist.html">博客管理系统</a><span lay-separator="">/</span>
			          	<a href="bloglist.html">后台首页</a><span lay-separator="">/</span>
			          	<a><cite>博文修改</cite></a>
		        	</span>
            </p>
        </div>
        <div class="layui-col-md12">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" id="t1" required  lay-verify="required" value="请输入标题" autocomplete="off" class="layui-input max_width500">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分类</label>
                    <div class="layui-input-block">
                        <select name="city" lay-verify="required" value=''>
                            <option value="" class="max_width500"></option>
                            <option value="JavaSE" class="max_width500">JavaSE</option>
                            <option value="DataBase" class="max_width500">DataBase</option>
                            <option value="jsp" class="max_width500">JSP</option>
                            <option value="Frame" class="max_width500">Frame</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" id="zhiDing">置顶</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="switch" lay-skin="switch" value="true" id="c1" >
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block" >
                        <textarea name="desc" placeholder="请输入文本内容" value="" class="layui-textarea" id="editor" style="height: 500px"></textarea>
                        <!--<textarea name="desc"  id="editor" placeholder="请输入文本内容"></textarea>-->

                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo" id="button">立即提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script src="static/layui/layui.all.js"></script>
<script src="static/js/jquery-1.12.4.min.js"></script>
<script src="static/admin/javascript/blogadd.js"></script>

<script type="text/javascript">
    /*盛志健的编辑功能*/
    $(document).ready(function () {


        //1.窗体加载后，发送异步请求：
        function getParameterByName(name) {
            var urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(name);
        }
        var blogid = getParameterByName('blogid');
        $.get("Blog/selectByBlogid", {"blogid": blogid}, function(rs) {
            JSON.stringify(rs); // 显示对象的JSON字符串形式
            /*把文章标题在前端页面展示*/
            document.querySelector('input[name="title"]').setAttribute('value', rs[0].title);

            $("[name='city']").val(rs[0].type);

            /*在里面添加数据*/
            /*$("textarea[name='desc']").prop('value',blogs[0].detail);*/
            /*把文章内容在前端显示*/
            $("#editor").val(rs[0].detail);
            if(rs[0].status=1) {
                /* $("#switch").prop('checked', true);*/
                /*$("[type='checkbox']").prop('checked', true);*/
                var checkbox =   document.getElementById("c1");
                checkbox.checked=true;
            }
        });
        /*修改数据并提交*/
        $("button[id='button']").click(function () {
            var t = $("[id='t1']").val();
            var type=$("[name='city']").val();
            var detail=$("#editor").prop('value');
            /*调用修改方法*/
            var status = $("#c1").prop("checked");
            alert(status);
            if (status == true) {
                status = 1;
            } else if (status == false) {
                status = 0;
            }
            $.get("Blog/updateBlogTextById",{
                    "blogid":blogid,
                    "title":t,
                    "type":type,
                    "status":status,
                    "detail":detail
                },
                function (rs) {
                    var blogs=rs.result;
                    alert(blogs);
                });
        });
        /* $.get("Blog/updateBlogTextById",{
                 "blogid":blogid,
                 "title":t,
                 "type":type,
                 "status":status,
                 "detail":detail
             },
             function (rs) {
                 var blogs=rs.result;
                 alert(blogs);
             });*/
        /* $("#switch").change(function () {
             alert("++++++++++++");
             /!*置顶操作*!/
             $.get("Top/showTop", function (result) {
                 console.log(result);
                 $("#title1").empty();
                 /!*检查 result 是否是数组*!/
                 if (Array.isArray(result)) {
                     for (var j = 0; j < result.length; j++) {
                         var item = result[j];
                         /!* 确保 item 包含所需的属性*!/
                         if (item.blogid && item.title && item.posttime && item.detail) {
                             $("<div class=\"main zdbox top-detail\"  name='" + item.blogid + "'>\n" +
                                 "<div class=\"subject\"><b>[置顶]</b><a title=\"置顶详情\"  name='" + item.blogid + "' >" + item.title + "</a><em>" + item.posttime + "\n" +
                                 "发布</em></div>\n" +
                                 "<div class=\"content\">" + item.detail + "\n" +
                                 "</div>\n" +
                                 "</div>").appendTo("#title1");
                         }
                     }
                 } else {
                     console.error("Unexpected data format:", result);
                 }
             }).fail(function() {
                 console.error("Failed to fetch data from server.");
             });
         });*/


        /*当他不做置顶选择时，会执行以下操作*/
        /*else {
            $("#switch").prop('checked',false);
            status=0;
            $.get("Blog/updateBlogTextById",{
                    "blogid":blogid,
                    "title":t,
                    "type":type,
                    "status":status,
                    "detail":detail
                },
                function (rs) {
                    var blogs=rs.result;
                    alert(blogs);
                });
        }*/

    });

</script>
</html>